

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class CannonPanel extends JPanel {

	CannonDynamics dyn;

	//* Create a main with the panel. Voeg code toe aan CannonPanel, zo kan je het kanon zien.
	/**
	  * @param args
	  */
	 public static void main(String[] args) {
	   JFrame f = new JFrame("Cannon Experiments frame");
	   f.setContentPane(new CannonPanel(true));
	   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	   f.pack();
	   Dimension frameSize = f.getSize();
	   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	   f.setLocation(screenSize.width / 2 - frameSize.width / 2, screenSize.height / 2 - frameSize.height / 2);
	   f.setVisible(true);
	 }

	public CannonPanel(boolean chooseAmmoRandom){

		dyn = new CannonDynamics(chooseAmmoRandom);

		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		add(new DrawPanel());

		new Thread(){
			public void run(){
				while(true){
					dyn.nextState();
					repaint();
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {}
				}
			}
		}.start();
	}

	class DrawPanel extends JPanel{
		final int X0 = 60, WIDTH = 600, HEIGHT=400;
		int[] groundXCoordinates = new int[dyn.FIELD_LENGTH+2];

		public DrawPanel(){
			for(int i=0;i<dyn.FIELD_LENGTH;i++)
				groundXCoordinates[i] = X0 + i;
			groundXCoordinates[dyn.FIELD_LENGTH] = X0 + dyn.FIELD_LENGTH;
			groundXCoordinates[dyn.FIELD_LENGTH+1] = X0 ;

			setPreferredSize(new Dimension(X0 + WIDTH, HEIGHT + dyn.DEPTH));
		}
		final Color BROWN = new Color(165, 65, 0),BROWN2 = new Color(150, 86, 16), GRAY = new Color(108, 108, 108);
		@Override
		protected void paintComponent(Graphics g){
			super.paintComponent(g);

			// cannonball
			g.setColor(Color.BLACK);
			 if (dyn.ctr>2)
			  g.fillOval(X0+(int)(dyn.posX-dyn.BALL_SIZE), HEIGHT -(int)(dyn.posY+dyn.BALL_SIZE), dyn.BALL_SIZE, dyn.BALL_SIZE); //(int)(dyn.posX+dyn.BALL_SIZE/2), (int)(dyn.posY+dyn.BALL_SIZE/2));



			// cannon
			 g.setColor(Color.black);
			 int[] x = {X0, X0+ (int)(dyn.CANNON_LENGTH * Math.cos(dyn.angle)), X0+(int)(dyn.CANNON_LENGTH * Math.cos(dyn.angle) - (dyn.BALL_SIZE+3) * Math.sin(dyn.angle)), X0+(int)(- (dyn.BALL_SIZE+3) * Math.sin(dyn.angle))};
			 int[] y = {HEIGHT, HEIGHT - (int)(dyn.CANNON_LENGTH * Math.sin(dyn.angle)), HEIGHT - (int)(dyn.CANNON_LENGTH * Math.sin(dyn.angle) + (dyn.BALL_SIZE+3) * Math.cos(dyn.angle)), (int)(HEIGHT - (dyn.BALL_SIZE+3) * Math.cos(dyn.angle))};
			 g.fillPolygon(x, y, 4);
			 g.setColor(BROWN2);

			// ground
			g.setColor(Color.yellow);
			g.fillPolygon(groundXCoordinates, dyn.groundDraw, groundXCoordinates.length);
			g.fillRect(0, dyn.HEIGHT, X0, dyn.DEPTH);

			// fire
			if (dyn.fire){
				g.setColor(Color.YELLOW);
				double R = 2000;
				double r = dyn.ctr * 39*dyn.ammunition/200;

				g.fillOval((int)(X0 + R*Math.cos(dyn.angle) - (dyn.BALL_SIZE+3)*Math.sin(dyn.angle)/2 -r/2),(int)( HEIGHT - R*Math.sin(dyn.angle)- (dyn.BALL_SIZE+3)*Math.cos(dyn.angle)/2 -r/2 ), (int)r, (int) r);

				g.setColor(Color.ORANGE);
				r/=1.5;
				g.fillOval((int)(X0 + R*Math.cos(dyn.angle) - (dyn.BALL_SIZE+3)*Math.sin(dyn.angle)/2 -r/2),(int)( HEIGHT - R*Math.sin(dyn.angle)- (dyn.BALL_SIZE+3)*Math.cos(dyn.angle)/2 -r/2 ), (int)r, (int) r);
				g.setColor(Color.RED);
				r/=1.5;
				g.fillOval((int)(X0 + R*Math.cos(dyn.angle) - (dyn.BALL_SIZE+3)*Math.sin(dyn.angle)/2 -r/2),(int)( HEIGHT - R*Math.sin(dyn.angle)- (dyn.BALL_SIZE+3)*Math.cos(dyn.angle)/2 -r/2 ), (int)r, (int) r);
			}
		}
	}
}